package com.example.graduatetest.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.graduatetest.R;
import com.example.graduatetest.bean.Icon;

import java.util.List;

public class ActionItemAdapter extends BaseAdapter {
    private List<Icon> iconList;
    private LayoutInflater layoutInflater;

    public ActionItemAdapter(Context context,List<Icon> iconList) {
       this.iconList = iconList;
       layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return iconList.size();
    }

    @Override
    public Object getItem(int position) {
        return iconList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.action_item, null);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.txt_icon);
            holder.imageView = (ImageView)convertView.findViewById(R.id.img_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Icon icon = iconList.get(position);
        if (icon != null) {
            holder.textView.setText(icon.getName());
            holder.imageView.setImageResource(icon.getId());
        }
        return convertView;
    }

    class ViewHolder{
        TextView textView;
        ImageView imageView;
    }
}
